package com.isoft.systemprovidor.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.systemprovidor.bean.ResponseData;
import com.isoft.systemprovidor.entity.PhoneBook;
import com.isoft.systemprovidor.service.PhoneBookService;
import com.isoft.systemprovidor.util.FileUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;


@CrossOrigin
@RestController
@Slf4j
@RequestMapping("/systemprovidor/phonebook")
public class PhoneBookController extends BaseController{
    @Autowired
    PhoneBookService phoneBookService;
    /*分页获取分页电话簿数据*/
    @GetMapping("/getallphonebook")
    public ResponseData getallPhoneBookPage(Integer current , Integer size ,String department, String username ,String job){
        return phoneBookService.getallPhoneBookPage(current , size ,department,username , job);
    }

    /*修改电话簿数据*/
    @PutMapping("/updatephonebook")
    public ResponseData<Boolean> changePhoneBook(@RequestBody PhoneBook phoneBook){
        return  returnBoolean(phoneBookService.changePhoneBook(phoneBook));
    }
    //验证手机号是否重复
    @GetMapping("/isexistphone")
    public ResponseData<Boolean> phoneIsExist(String phone){
        return returnBoolean(phoneBookService.phoneIsExist(phone));
    }

    /*导入电话簿信息*/
    @Value("${myfile.user.phonebook}")
    private String savePath;
    @PostMapping("/importphonebook")
    public ResponseData<String> importPhoneBook(@RequestParam("phonebook") MultipartFile file , HttpServletRequest request){
        boolean result = false;
        String fileUrl = null;
        if (null != file){
            String newName="phonebook.xlsx";
            fileUrl = FileUtil.uploadPhyPath(file , newName ,savePath , request);
            log.info("存入物理路径:" + fileUrl);
            if (null != fileUrl){
                try {
                    // 调用服务层方法处理上传的文件
                    phoneBookService.importPhoneBook(fileUrl);
                    result = true;
                } finally {
                    // 无论成功或失败，尝试删除文件
                    File uploadedFile = new File(fileUrl);
                    if (uploadedFile.exists()) {
                        if (uploadedFile.delete()) {
                            log.info("暂存文件删除成功: " + fileUrl);
                        } else {
                            log.error("暂存文件删除失败: " + fileUrl);
                        }
                    }
                }
            }
        }
        log.info("文件上传路径:"+fileUrl);
        ResponseData<String> responseData = new ResponseData<>();
        responseData.setData(fileUrl);
        if (result){
            responseData.setErrorCode(200);
            responseData.setMsg("文件上传成功");
        }else {
            responseData.setErrorCode(605);
            responseData.setMsg("文件上传失败");
        }
        return responseData;

    }
    /*导出电话簿信息*/


}
